package com.zjhn.ds.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zjhn.ds.utils.CommonUtil;
import com.zjhn.ds.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @BelongsProject: equipment-and-energy
 * @BelongsPackage: com.zjhn.admin.config
 * @Author: sxt
 * @CreateTime: 2023-06-02  09:40
 * @Description: 自定义填充字段
 * @Version: 1.0
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Resource
    private UserUtil userUtil;

    /**
     * 新增策略
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, getCreateTime(), Date.class, getTime());
        this.strictInsertFill(metaObject, getCreateUser(), String.class, getUserValue());
        this.strictInsertFill(metaObject, getUpdateTime(), Date.class, getTime());
        this.strictInsertFill(metaObject, getUpdateUser(), String.class, getUserValue());

    }

    /**
     * 更新策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, getUpdateTime(), Date.class, getTime());
        this.strictUpdateFill(metaObject, getUpdateUser(), String.class, getUserValue());
    }

    private String getCreateTime() {
        return "createTime";
    }
    private String getCreateUser() {
        return "createUser";
    }
    private String getUpdateTime() {
        return "updateTime";
    }
    private String getUpdateUser() {
        return "updateUser";
    }
    private Date getTime() {
        return new Date();
    }
    private String getUserValue(){
        // 获取当前登录用户id
        return userUtil.getUserInfo().getAbpUserId();
    }
}
